package view.GUI.adminView.inventory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import model.Configuration;
import model.data.wrapper.InputWrapper;
import enums.GUIText;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Separator;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import view.GUI.mainView.KindVBox;
import view.GUI.mainView.KindVBox.BoxType;
import view.GUI.mainView.MainView;


public final class OrderTaking extends Application{
	
	private Label lblTopic;
	
	private Button btnTake;
	private Button btnCancel;
	
	private KindVBox vboxDrinks;
	private KindVBox vboxGrocery;
	private KindVBox vboxOther;
	
	private VBox mainVBox;
	
	private GridPane pane;
	
	private ScrollPane sp;
	
	private MainView owner;
	
	private Scene scene;
	private Stage primaryStage;
	private Separator sepBottom; 
	
	
	public OrderTaking(MainView mainView){
		this.owner = mainView;
		createWidgets();
		addWidgets();
		setupLayout();
		setupInteraction();
		
		start(new Stage());
	}
	
	private void createWidgets(){
		sepBottom = new Separator();
		mainVBox = new VBox();
		pane = new GridPane();
		
		lblTopic = new Label(GUIText.TAKEANORDER.toString());
		
		vboxDrinks = new KindVBox(BoxType.DRINKS, owner, owner.getProducts(true, BoxType.DRINKS));
		vboxGrocery = new KindVBox(BoxType.GROCERIES, owner, owner.getProducts(true, BoxType.GROCERIES));
		vboxOther = new KindVBox(BoxType.OTHER, owner, owner.getProducts(true, BoxType.OTHER));
		
		btnCancel = new Button(GUIText.CANCEL.toString());
		btnTake = new Button(GUIText.TAKEANORDER.toString());
	
		sp = new ScrollPane();
	}
	
	private void addWidgets(){
		mainVBox.getChildren().add(lblTopic);
		mainVBox.getChildren().add(vboxDrinks);
		mainVBox.getChildren().add(vboxGrocery);
		mainVBox.getChildren().add(vboxOther);
		
		pane.add(mainVBox, 0, 0);
		pane.add(btnTake, 0, 1);
		pane.add(btnCancel, 1, 1);
		pane.add(sepBottom, 0, 2);
		
		sp.setContent(pane);
	}
	
	private void setupLayout(){
		GridPane.setColumnSpan(sepBottom, 5);
		GridPane.setColumnSpan(mainVBox, 5);
		lblTopic.setId("topic");
		pane.getStyleClass().add("grid");
	}
	
	private void setupInteraction(){
		btnCancel.setOnAction(new EventHandler<ActionEvent>(){

			@Override
			public void handle(ActionEvent arg0) {
				primaryStage.close();
			}
		});
		
		btnTake.setOnAction(new EventHandler<ActionEvent>(){

			@Override
			public void handle(ActionEvent arg0) {
				takeOrder();
			}
		});
	}

	private void takeOrder() {
		Map<BoxType, List<InputWrapper>> order = new HashMap<>();
		
		order.put(BoxType.DRINKS, vboxDrinks.getInput());
		order.put(BoxType.GROCERIES, vboxGrocery.getInput());
		order.put(BoxType.OTHER, vboxOther.getInput());
		owner.takeOrder(order);
		primaryStage.close();
	}
	
	/**
	 * This method is needed in order to display the Login-Dialog.
	 */
	@Override
	public void start(Stage primaryStage) {

		primaryStage.setTitle(GUIText.OVERVIEW.toString());
		this.primaryStage = primaryStage;
		scene = new Scene(sp);
		scene.getStylesheets().add(getClass().getResource(Configuration.STYLEMAINVIEW).toExternalForm());
		scene.getStylesheets().add(getClass().getResource(Configuration.STYLEROOT).toExternalForm());
		this.primaryStage.initModality(Modality.WINDOW_MODAL);
		primaryStage.initOwner(owner.getPrimaryStage());
		primaryStage.setScene(scene);
		primaryStage.setResizable(true);
		primaryStage.setWidth(650);
		primaryStage.setHeight(600);
		primaryStage.centerOnScreen();
		primaryStage.show();
	}
}
